const Goods = require('../models/goods');
const discountCoupon = require('../models/discount');
const {Op}=require('sequelize');

// 添加商品
exports.addGoods=async function(params){
    const result=await Goods.create(params);
    return result
}
// 查询商品
exports.findGoods=async function(category ,pagenum=1 ,pagesize=-1){
    const total = await Goods.count();
    limit=pagesize==-1?total:pagesize;
    const result = await Goods.findAll({
        attributes: ['id','name','price','brief','img','discount','salesVolume'],
        where: {
            category: {
                [Op.like]: category?category:'%'
            }
        },
        offset:(pagenum -1)*limit,
        limit: +limit
    })
    return {result,total}
}
// 查询商品详细信息
exports.findMore=async function(id){
    const result = await Goods.findAll({
        where:{
            id
        }
    })
    return result
}
// 查询折扣列表
exports.findDiscount=async function(){
    const result = await discountCoupon.findAll();
    return result
}

exports.addDiscount=async function(params){
    return await discountCoupon.create(params);
}
